package com.wenhui.schoolyardinfo.message.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnLongItemTripletsViewClickListener;
import com.wenhui.schoolyardinfo.customview.CircleImageView;
import com.wenhui.schoolyardinfo.manager.DateManager;
import com.wenhui.schoolyardinfo.message.bean.PrivateLetterBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/6/26 10:43
 * introduce : 私信Adapter
 */

public class PrivateLetterAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<PrivateLetterBean> privateLetterBeanList = new ArrayList<>();
    private static final int HEADER_FIRST_VIEW_TYPE = 1;
    private static final int HEADER_SECOND_VIEW_TYPE = 2;
    private static final int CONTENT_VIEW_TYPE = 3;

    public PrivateLetterAdapter(Context context) {
        this.context = context;
    }

    public PrivateLetterAdapter(Context context, List<PrivateLetterBean> privateLetterBeanList) {
        this.context = context;
        this.privateLetterBeanList = privateLetterBeanList;
    }

    public void clearData() {
        privateLetterBeanList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<PrivateLetterBean> privateLetterBeanList) {
        this.privateLetterBeanList.addAll(privateLetterBeanList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case HEADER_FIRST_VIEW_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_private_letter_header_first, parent, false);
                return new HeaderFirstHolder(view);
            case HEADER_SECOND_VIEW_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_private_letter_header_second, parent, false);
                return new HeaderSecondHolder(view);
            default:
                view = LayoutInflater.from(context).inflate(R.layout.item_private_letter_content, parent, false);
                return new ContentHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderFirstHolder) {
            HeaderFirstHolder headerFirstHolder = (HeaderFirstHolder) holder;

        } else if (holder instanceof HeaderSecondHolder) {
            HeaderSecondHolder headerSecondHolder = (HeaderSecondHolder) holder;
        } else {
            ContentHolder contentHolder = (ContentHolder) holder;
            //加载头像
            Glide.with(context).load(privateLetterBeanList.get(position - 2).getHeadPortraitUrl()).into(contentHolder.imvHeadPortrait);
            contentHolder.tevName.setText(privateLetterBeanList.get(position - 2).getName());
            contentHolder.tevChatMessage.setText(privateLetterBeanList.get(position - 2).getChatMessage());
            contentHolder.tevTime.setText(DateManager.millisecondConvertedToDate(privateLetterBeanList.get(position - 2).getTime()));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEADER_FIRST_VIEW_TYPE;
        } else if (position == 1) {
            return HEADER_SECOND_VIEW_TYPE;
        } else {
            return CONTENT_VIEW_TYPE;
        }
    }

    @Override
    public int getItemCount() {
        return privateLetterBeanList.size() + 2;
    }

    protected class HeaderFirstHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.imv_head_portrait)
        CircleImageView imvHeadPortrait;
        @BindView(R.id.tev_name)
        TextView tevName;
        @BindView(R.id.tev_chat_message)
        TextView tevChatMessage;
        @BindView(R.id.layout_new_friends)
        RelativeLayout layoutNewFriends;

        private HeaderFirstHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutNewFriends.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_new_friends:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    protected class HeaderSecondHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.imv_head_portrait)
        CircleImageView imvHeadPortrait;
        @BindView(R.id.tev_name)
        TextView tevName;
        @BindView(R.id.tev_chat_message)
        TextView tevChatMessage;
        @BindView(R.id.layout_group_chat)
        RelativeLayout layoutGroupChat;

        private HeaderSecondHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutGroupChat.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_group_chat:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    protected class ContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        @BindView(R.id.imv_head_portrait)
        CircleImageView imvHeadPortrait;
        @BindView(R.id.tev_name)
        TextView tevName;
        @BindView(R.id.tev_chat_message)
        TextView tevChatMessage;
        @BindView(R.id.tev_time)
        TextView tevTime;
        @BindView(R.id.view_private_letter)
        View viewPrivateLetter;
        @BindView(R.id.layout_chat_message)
        RelativeLayout layoutChatMessage;

        private ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutChatMessage.setOnClickListener(this);
            layoutChatMessage.setOnLongClickListener(this);
            imvHeadPortrait.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_chat_message:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.imv_head_portrait:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }

        @Override
        public boolean onLongClick(View view) {
            switch (view.getId()) {
                case R.id.layout_chat_message:
                    rcvOnLongItemTripletsViewClickListener.onLongItemClickListener(getAdapterPosition(), view, tevName, tevChatMessage);
                    break;
                default:
                    break;
            }
            return true;
        }
    }

    private RcvOnItemViewClickListener rcvOnItemViewClickListener;

    public void setRcvOnItemViewClickListener(RcvOnItemViewClickListener rcvOnItemViewClickListener) {
        this.rcvOnItemViewClickListener = rcvOnItemViewClickListener;
    }

    private RcvOnLongItemTripletsViewClickListener rcvOnLongItemTripletsViewClickListener;

    public void setRcvOnLongItemTripletsViewClickListener(RcvOnLongItemTripletsViewClickListener rcvOnLongItemTripletsViewClickListener) {
        this.rcvOnLongItemTripletsViewClickListener = rcvOnLongItemTripletsViewClickListener;
    }
}
